package com.qf.controller;

import com.alibaba.druid.util.StringUtils;

import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsRole;
import com.qf.service.AdminService;
import com.qf.service.PermissionService;
import com.qf.service.RoleService;
import com.qf.util.Permission;
import com.qf.util.PermissionUtil;
import com.qf.util.ResponseUtil;

import com.qf.vo.PermVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/admin")
@Slf4j
public class AdminRoleController {



    @Autowired
    RoleService roleService;
    @Autowired
    ApplicationContext context;
    @Autowired
    PermissionService permissionService;

    //角色授权
    @GetMapping("/role/permissions")
    public Object getPower(Integer roleId){
        //systemPermissions  系统权限（所有的权限） List<PermVo>
        List<Permission> permissions = PermissionUtil.listPermission(context, "com.qf.controller");
        //获取系统权限
        List<PermVo> systemPermissions = PermissionUtil.listPermVo(permissions);


        //assignedPermissions  该角色已经拥有的权限 类型List<String>
        //如果是超级管理员，则拥有所有的权限
        boolean flag = roleService.isSuperAdmin(roleId);
        Set<String> assignedPermissions = null;
        if(flag) {
            //获取系统所有权限字符串
            assignedPermissions = PermissionUtil.listPermissionString(permissions);

        }
        //如果不是超级管理员，在数据库查找相应的权限
        else {
            assignedPermissions = permissionService.queryByIds(new Integer[]{roleId});
        }

        Map<String,Object> data = new HashMap<>();
        data.put("systemPermissions",systemPermissions);
        data.put("assignedPermissions", assignedPermissions);
        return ResponseUtil.ok(data);
     /*   //系统权限，得到所有的权限
        List<Permission> permissions = PermissionUtil.listPermission(context,"com.qf.controller");
        //获取系统权限
        List<PermVo> permVoList = PermissionUtil.listPermVo(permissions);

        //assignedPermissions  该角色已经拥有的权限 类型List<String>
        //如果是超级管理员，则拥有所有的权限
        boolean flag = roleService.isSuperAdmin(roleId);
        Set<String> assignedPermission=null;
        if(flag){
            System.out.println(flag);
            assignedPermission = PermissionUtil.listPermissionString(permissions);
        }
        else{
            assignedPermission = permissionService.queryByIds(new Integer[]{roleId});
        }
        Map<String,Object> data = new HashMap<>();
        data.put("systemPermissions",permVoList);
        data.put("assignedPermissions",assignedPermission);
        return ResponseUtil.ok(data);*/

    }

    @PostMapping("/role/permissions")
    public Object updatePermissions(@RequestBody Map<String,Object> map){
        int roleId = Integer.parseInt(String.valueOf(map.get("roleId")));
        List<String> permisssions= (List<String>) map.get("permissions");
        if(roleService.isSuperAdmin(roleId)){
            permisssions = new ArrayList<>();
            permisssions.add("*");
        }
        permissionService.insert(roleId,permisssions);

        return ResponseUtil.ok();

    }








    @RequestMapping("/role/options")
    public Object getAll(){

        List<DtsRole> list = roleService.getAll();
        List<Map<String,Object>> data = list.stream().map(role -> {
            Map<String,Object> map = new HashMap<>();
            map.put("label", role.getName());
            map.put("value", role.getId());
            return map;
        }).collect(Collectors.toList());

        return ResponseUtil.ok(data);
        }

    @RequiresPermissions("admin:role:list")
    @RequiresPermissionsDesc(menu = { "系统管理", "角色管理" }, button = "角色查询")
    @GetMapping("/role/list")
        public Object list(@RequestParam(value = "page",required = false,defaultValue = "1") Integer page,
                           @RequestParam(value = "limit",required = false,defaultValue = "3") Integer limit,
                           @RequestParam(value = "sort",required = false,defaultValue = "add_time") String sort,
                           @RequestParam(value = "order",required = false,defaultValue = "desc") String order,String roleName){
        Map<String,Object> map =  roleService.selectList(page,limit,sort,order,roleName);
        return ResponseUtil.ok(map);


        }


    @RequiresPermissions("admin:role:create")
    @RequiresPermissionsDesc(menu = { "系统管理", "角色管理" }, button = "角色添加")
    @PostMapping("/role/create")
        public Object create(@RequestBody DtsRole role){


        role.setAddTime(new Date());
        role.setDeleted(false);
        role.setUpdateTime(new Date());
        roleService.create(role);

        return ResponseUtil.ok(role);


        }

    @RequiresPermissions("admin:role:update")
    @RequiresPermissionsDesc(menu = { "系统管理", "角色管理" }, button = "角色编辑")
    @PostMapping("/role/update")
    public Object update(@RequestBody DtsRole role){

        roleService.update(role);
        return ResponseUtil.ok();
        }

    @RequiresPermissions("admin:role:delete")
    @RequiresPermissionsDesc(menu = { "系统管理", "角色管理" }, button = "角色删除")
    @PostMapping("/role/delete")

    public Object deleted(@RequestBody DtsRole role){

        roleService.delete(role);

        return ResponseUtil.ok();
        }
    }












